import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'home',
      component: HomeView,
    },
    {
      path: '/about',
      name: 'about',
      component: () => import('../views/AboutView.vue'),
    },
    {
      path: '/login',
      name: 'Login',
      component: () => import('../views/Login.vue'),
    },
    {
      path: '/system/roles',
      name: 'DoctorList',
      component: () => import('../views/DoctorPermissionView.vue'),
    },
    {
      path: '/doctor/list',
      name: 'DoctorList',
      component: () => import('../views/doctor/DoctorList.vue'),
    },
    {
      path: '/doctor/schedule',
      name: 'doctorSchedule',
      component: () => import('../views/doctor/DoctorSchedule.vue')
    }
  ],
})

// 全局前置守卫，验证是否登录
router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('token');
  
  // 如果是登录页面，直接放行
  if (to.path === '/' || to.path === '/login') {
    next();
  } else if (!token) {
    // 没有token，跳转到登录页
    next('/');
  } else {
    // 有token，继续访问
    next();
  }
});

export default router
